#include"stack.h"

void InitStack(Stack* ps) {
	assert(ps);
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * Init_size);
	if (ps->a == NULL) {
		perror("malloc false");
		return;
	}
	ps->capacity = Init_size;
	ps->top = 0;
}

bool IsEmpty(Stack* ps) {
	assert(ps);
	return ps->top == 0;
}

void pushStack(Stack* ps, STDatatype x) {
	assert(ps);
	if (ps->top == ps->capacity) {
		STDatatype* tmp = (STDatatype*)realloc(ps->a, sizeof(STDatatype) * ps->capacity * 2);
		if (tmp == NULL) {
			perror("realloc false");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	ps->a[ps->top++] = x;
}

void popStack(Stack* ps) {
	assert(ps);
	assert(!IsEmpty(ps));
	ps->top--;
}

STDatatype getpopStack(Stack* ps) {
	assert(ps);
	assert(!IsEmpty(ps));
	return ps->a[ps->top - 1];
}

void DestroyStack(Stack* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}